
import json
import collections
import os, re

f = open("e:\\w\\2.csv", 'r' ,encoding='utf8')
fo = open("e:\\w\\forms.csv", 'w' ,encoding='utf8')

words = {}

for line in f.readlines():
    items=re.split("\|",line)

    word     = items[0] if len(items) >= 1 else None
    pos_str  = items[1] if len(items) >= 2 else ""

    plural                =  items[2] if len(items) >= 3  else None
    comparative           =  items[3] if len(items) >= 4  else None
    superlative           =  items[4] if len(items) >= 5  else None
    third_person_singular =  items[5] if len(items) >= 6  else ""
    present_participle    =  items[6] if len(items) >= 7  else ""
    past_participle       =  items[7] if len(items) >= 8  else ""
    past_tense            =  items[8] if len(items) >= 9  else ""
    singular              =  items[9] if len(items) >= 10 else None

    pos_items=re.split(r'#',pos_str)
    is_n = False
    is_v = False

    pos_map = {
        "vt."      : "v",
        "v."       : "v",
        "vi."      : "v",
        "modal.v." : "v",
        "link.v."  : "v",
        "aux.v."   : "v",
        "n."       : "n",
    }

    pos_list = []
    for item in pos_items:
        if pos_map.get(item) == "v":
            is_v = True
            pos_list.append(item)
        elif pos_map.get(item) == "n":
            is_n = True
            #pos_list.append(item)

    pos_list = list(set(pos_list))

    # 不要adj, adv
    if len(pos_list) < 1: continue
    if is_v is False : continue

    word_info = words.get(word)
    if word_info is None:
        words[word] = {}
        word_info = words.get(word)
        word_info['forms'] = {}

    if word_info.get("present_participle") is None: word_info["present_participle"] = {}
    v_items=re.split(r',',present_participle)
    for v in v_items:
        if len(v):
            word_info["present_participle"][v] = v
            word_info['good']=True
            word_info['forms'][v] = v

    if word_info.get("past_participle") is None: word_info["past_participle"] = {}
    v_items=re.split(r',',past_participle)
    for v in v_items:
        if len(v):
            word_info["past_participle"][v] = v
            word_info['good']=True
            word_info['forms'][v] = v

    if word_info.get("past_tense") is None: word_info["past_tense"] = {}
    v_items=re.split(r',',past_tense)
    for v in v_items:
        if len(v):
            word_info["past_tense"][v] = v
            word_info['good']=True
            word_info['forms'][v] = v

    if word_info.get("third_person_singular") is None: word_info["third_person_singular"] = {}
    v_items=re.split(r',',third_person_singular)
    for v in v_items:
        if len(v):
            word_info["third_person_singular"][v] = v
            word_info['good']=True
            word_info['forms'][v] = v

    #if is_n and plural:
    #    if word_info.get("plural") is None:
    #        word_info["plural"] = {}
    #    word_info["plural"][plural] = plural

#for word, word_info in words.items():
#    if word_info.get("plural"):
#        for p,v in word_info["plural"].items():
#            if p != word:
#                word_plural = words.get(p)
#                if word_plural:
#                    words[p] = {}

for word, word_info in words.items():
    if word_info.get("good"):
        for p,v in word_info["forms"].items():
            if p != word:
                word_plural = words.get(p)
                if word_plural:
                    words[p]["good"] = False

for word, word_info in words.items():
    if word_info.get('good') is True:
        fo.write(word+"|"+",".join(p for p,v in word_info["present_participle"].items())+"|"+",".join(p for p,v in word_info["past_participle"].items())+"|"+",".join(p for p,v in word_info["past_tense"].items())+"|"+",".join(p for p,v in word_info["third_person_singular"].items())+"\n")


fo.close()

